<?php

namespace App\Jobs;

use App\Models\Mint;
use App\Models\Nft;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

class GetUserNftJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(public string $nftId, public array $metadata, public string $owner, public string $img)
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $jsonStr = File::get(app_path('nft/attribute_percent.json'));
        $data = json_decode($jsonStr, true);
        DB::transaction(function () use ($data) {
            $nft = Nft::where('nft_id', $this->nftId)->first();
            //拥有者
            $user = User::firstOrCreate(['address' => $this->owner]);
            if ($nft) {
                // Update NFT
                $nft->user_id = $user->id;
                $nft->save();

                // Update Mint
                $mints = Mint::where('nft_id', $this->nftId)->where('status', 'waiting')->get();
                foreach ($mints as $mint) {
                    $mint->user_id = $user->id;
                    $mint->save();
                }
            } else {
                $nft = Nft::where('name', $this->metadata['name'])->first();
                if ($nft) {
                    $nft->nft_id = $this->nftId;
                    $nft->user_id = $user->id;
                    $nft->save();
                    // Update Mint
                    $mints = Mint::where('nft_id', $this->nftId)->where('status', 'waiting')->get();
                    foreach ($mints as $mint) {
                        $mint->user_id = $user->id;
                        $mint->save();
                    }
                } else {
                    // Create new NFT
                    $attributes = $this->metadata['attributes'];
                    foreach ($attributes as $attribute) {
                        if ($attribute['trait_type'] == 'Background') {
                            $background_percent = $data['backgrounds'][$attribute['value']] ?? 0;
                            $background = $attribute['value'];
                        } elseif ($attribute['trait_type'] == 'Core') {
                            $core_percent = $data['cores'][$attribute['value']] ?? 0;
                            $core = $attribute['value'];
                        } elseif ($attribute['trait_type'] == 'Gear') {
                            $gear_percent = $data['gears'][$attribute['value']] ?? 0;
                            $gear = $attribute['value'];
                        } elseif ($attribute['trait_type'] == 'Hat') {
                            $hat_percent = $data['hats'][$attribute['value']] ?? 0;
                            $hat = $attribute['value'];
                        } elseif ($attribute['trait_type'] == 'Traits') {
                            $traits_percent = $data['traits'][$attribute['value']] ?? 0;
                            $traits = $attribute['value'];
                        } elseif ($attribute['trait_type'] == 'Earring') {
                            $earring_percent = $data['earrings'][$attribute['value']] ?? 0;
                            $earring = $attribute['value'];
                        } elseif ($attribute['trait_type'] == 'HashRate') {
                            $power = $attribute['value'];
                        }
                    }
                    $nft = new Nft();
                    $nft->user_id = $user->id;
                    $nft->nft_id = $this->nftId;
                    $nft->name = $this->metadata['name'];
                    $nft->symbol = $this->metadata['symbol'];
                    $nft->description = $this->metadata['description'];
                    $nft->img = $this->img;
                    $nft->background = $background;
                    $nft->core = $core;
                    $nft->gear = $gear;
                    $nft->hat = $hat;
                    $nft->traits = $traits;
                    $nft->earring = $earring;
                    $nft->background_percent = $background_percent ?? 0;
                    $nft->core_percent = $core_percent ?? 0;
                    $nft->gear_percent = $gear_percent ?? 0;
                    $nft->hat_percent = $hat_percent ?? 0;
                    $nft->traits_percent = $traits_percent ?? 0;
                    $nft->earring_percent = $earring_percent ?? 0;
                    $nft->percent = bcmul(bcmul(bcmul($background_percent, $core_percent), $gear_percent), bcmul($hat_percent, bcmul($traits_percent, $earring_percent)));
                    $nft->power = $power ?? '10';
                    $nft->days = 365;
                    $nft->save();
                }
            }
        });
    }
}
